//获取应用实例
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    city:"点击选择",
    date:"点击选择",
    // 选择的图片装货
    selectImg: [],

    //最终上传的图片
    imgs: []

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getUserInfo({
      success: function (res) {
        console.log(res);
        var nickName = res.userInfo.nickName
        var avatarUrl = res.userInfo.avatarUrl
        that.setData({
          avatarUrl: avatarUrl,
          nickName: nickName
        })
      }
    })
  },
  bindDateChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
    })
  },
  submit:function(res){

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    that.setData({
      city: app.globalData.city1
    })
  },
  // 上传图片
  uploadImg: function (img, fn) {
    var that = this;
    var imgs = that.data.imgs;
    app.getLoginSession(function (session) {
      wx.uploadFile({
        url: app.api,
        filePath: img,
        name: 'file',
        formData: {
          app: 'member',
          act: 'upload_file',
          filePath: img,
          t_session: session
        },
        method: 'POST',
        header: {
          'content-type': 'multipart/form-data'
        },
        success: function (res) {
          var data = JSON.parse(res.data);
          var rlt = data.retval;
          if (data.done) {

            imgs.push(rlt.save_path);
            that.setData({
              imgs: imgs
            })
            console.log("sss" + that.data.imgs)
            typeof fn == 'function' && fn();
          } else {
            console.log(data);
            that.tips('图片上传失败')
          }
        },
        fail: function () {
          console.log("失败");
        }
      })
    })
  },
  // 选择图片
  chooseImg: function (res) {
    var that = this;
      that.setData({
        selectImg: []
      })
      var selectImg = that.data.selectImg;
      wx.chooseImage({
        count: 1,
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success: function (res) {

          var tempFilePaths = res.tempFilePaths

          if (tempFilePaths.length + selectImg.length > 5) {
            that.tips('最多只能上传5张')
          } else {
            for (var i = 0; i < tempFilePaths.length; i++) {
              selectImg.push(tempFilePaths[i])
              // that.uploadImg(tempFilePaths[i])
            }
            that.setData({
              selectImg: selectImg
            })
            console.log(selectImg);
          }

        },
        fail: function () {
          that.tips('选取图片失败')
        }
      })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})